/*
*/
#include "board.h"
#include "mb.h"
#include "MotorControl.h"

extern int16_t g_s16PwmDuty;


void demo_tim1_main(void);
void demo_uart3(void);
void demo_freemodbus(void);
void demo_h_bridge(void);
void demo_Tim2Enc(void);
void demo_Enc(void);
void demo_softtimer(void);
void demo_motor(void);
void demo_pid(void);
void demo_sync_spin(void);


void demo_all(void) {
    
    // mode, slave addr, port, baud, parity
    eMBInit(MB_RTU, 0x01, 0x00, 9600, MB_PAR_NONE);
    eMBEnable();
    
    Mot_InitSys();
    
    g_s16PwmDuty = 500;
    
    M1.ParamRW.s16ReductionRatio = 20;

    M1.pstHBdg->ParamChanged = true;
    M1.pstHBdg->Param.State = HB_State_Run;
    M1.pstHBdg->Param.DirMode = HB_DirMode_ALBHasForward;
    M1.pstHBdg->Param.Dir = HB_Dir_Backward;
    
    M1.pstEnc->ParamChanged = true;
    M1.pstEnc->Param.DirMode = Enc_DirMode_0;
    M1.pstEnc->Param.PulsPerRev = 13;
    
    M2.ParamRW.s16ReductionRatio = 35;

    M2.pstHBdg->ParamChanged = true;
    M2.pstHBdg->Param.State = HB_State_Run;
    M2.pstHBdg->Param.DirMode = HB_DirMode_ALBHasForward;
    M2.pstHBdg->Param.Dir = HB_Dir_Backward;
    
    M2.pstEnc->ParamChanged = true;
    M2.pstEnc->Param.DirMode = Enc_DirMode_0;
    M2.pstEnc->Param.PulsPerRev = 11;
    
    M3.ParamRW.s16ReductionRatio = 150;

    M3.pstHBdg->ParamChanged = true;
    M3.pstHBdg->Param.State = HB_State_Run;
    M3.pstHBdg->Param.DirMode = HB_DirMode_AHBLasForward;
    M3.pstHBdg->Param.Dir = HB_Dir_Backward;
    
    M3.pstEnc->ParamChanged = true;
    M3.pstEnc->Param.DirMode = Enc_DirMode_1;
    M3.pstEnc->Param.PulsPerRev = 7;
    
    M4.ParamRW.s16ReductionRatio = 48;

    M4.pstHBdg->ParamChanged = true;
    M4.pstHBdg->Param.State = HB_State_Run;
    M4.pstHBdg->Param.DirMode = HB_DirMode_ALBHasForward;
    M4.pstHBdg->Param.Dir = HB_Dir_Backward;
    
    M4.pstEnc->ParamChanged = true;
    M4.pstEnc->Param.DirMode = Enc_DirMode_1;
    M4.pstEnc->Param.PulsPerRev = 13;
    
    while (1) {
        eMBPoll();
        
//        HBridge_SetPwmDuty(&hstHBdg_1, g_s16PwmDuty);
        Mot_Update(&M1);
        Mot_Update(&M2);
        Mot_Update(&M3);
        Mot_Update(&M4);
    }
}


int main() {
    board_EnableAllClocks();
    board_ConfigAllClocks();
    initBoard();
    
//    demo_tim1_main();
//    demo_uart3();
//    demo_freemodbus();
//    demo_h_bridge();
//    demo_Tim2Enc();
//    demo_Enc();
//    demo_softtimer();
//    demo_motor();
//    demo_pid();
    demo_sync_spin();
    
    demo_all();
}
